<?php

namespace App\Services\AMQP;

use App\Util\LockUtil;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

class AMQPProduce
{
    //测试处理-pay-order
    const DEV_PAY_URL = "http://121.40.18.93:19501/payAdvice/produce";

    //正式处理-pay-order
    const PRO_PAY_URL = "http://121.40.18.93:9501/payAdvice/produce";

    //测试处理-refund-order
    const DEV_REFUND_URL = "http://121.40.18.93:19501/payAdvice/produceRefund";

    //正式处理-refund-order
    const PRO_REFUND_URL = "http://121.40.18.93:9501/payAdvice/produceRefund";

    /**
     * @param array $data
     * @param $action "pay"  "refund"
     * @param string $env "dev" "pro"
     * @return void
     */
    public function run(array $data, $action = "pay", string $env = "dev"): void
    {
        try {
            Log::channel('pay')->info("env_" . $env . ":action_" . $action . date("Y-m-d H:i:s", time()) . "_start", $data);
            switch ($action) {
                case "pay":
                    $url = self::DEV_PAY_URL;
                    if ($env == "pro") {
                        $url = self::PRO_PAY_URL;
                    }
//                    $ret = $this->curl($url, "POST", [
//                        "out_trade_no" => $data["out_trade_no"] ?? "",
//                    ]);
                    $this->produce($data["out_trade_no"], 1);
                    break;
                case "refund":
                    $url = self::DEV_REFUND_URL;
                    if ($env == "pro") {
                        $url = self::PRO_REFUND_URL;
                    }
//                    $this->curl($url, "POST", [
//                        "out_trade_no" => $data["out_trade_no"] ?? "",
//                    ]);
                    $this->produce($data["out_trade_no"], 2, $data["refund_no"] ?? "");
                    break;
            }
        } catch (\Throwable $e) {
            Log::channel('amqp')->error("env_" . $env . ":action_" . $action . date("Y-m-d H:i:s", time()) . "_error", [
                "out_trade_no" => $data["out_trade_no"] ?? "",
                "err_msg" => $e->getMessage()
            ]);
        }
    }


    public function produce(string $out_trade_no, int $type, string $refund_no = ""): void
    {
        $data = [
            "out_trade_no" => $out_trade_no,
            "type" => $type,
             "from" => "pay",
        ];
        if ($type == 2) {
            $data["refund_no"] = $refund_no;
        }
        $redis = Redis::connection('amqpQueue');
        $redis->rPush("payListener", json_encode($data));
        Log::channel('amqp')->info("payListener:", $data);
    }

    public function curl($url, $method, $params, $header = []): array
    {
        //初始化CURL句柄
        $curl = curl_init();

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($header)) {
            curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        }
        //请求时间
        $timeout = 2;
        curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
        switch ($method) {
            case "GET" :
                curl_setopt($curl, CURLOPT_HTTPGET, true);
                break;
            case "POST":
                if (is_array($params)) {
                    $params = json_encode($params, 320);
                }
                curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type:application/json']);
                curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
                curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
                break;
        }
        $data = curl_exec($curl);
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);//关闭cURL会话

        return json_decode($data, true);
    }
}
